<template><div><h2 id="redis" tabindex="-1"><a class="header-anchor" href="#redis"><span>Redis</span></a></h2>
<ul>
<li><a href="#introduction">安装</a></li>
<li><a href="#configuration">配置</a>
<ul>
<li><a href="#clusters">集群</a></li>
<li><a href="#predis">Predis</a></li>
<li><a href="#phpredis">phpredis</a></li>
</ul>
</li>
<li><a href="#interacting-with-redis">与Redis交互</a>
<ul>
<li><a href="#transactions">事务</a></li>
<li><a href="#pipelining-commands">管道命令</a></li>
</ul>
</li>
<li><a href="#pubsub">发布与订阅</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p><a href="https://redis.io/" target="_blank" rel="noopener noreferrer">Redis</a> 是一个开源的, 高级键值对存储数据库。 保护的数据库类型有 <a href="https://redis.io/topics/data-types#strings" target="_blank" rel="noopener noreferrer">字符串</a>, <a href="https://redis.io/topics/data-types#hashes" target="_blank" rel="noopener noreferrer">hash</a>, <a href="https://redis.io/topics/data-types#lists" target="_blank" rel="noopener noreferrer">列表</a>, <a href="https://redis.io/topics/data-types#sets" target="_blank" rel="noopener noreferrer">集合</a> 和 <a href="https://redis.io/topics/data-types#sorted-sets" target="_blank" rel="noopener noreferrer">有序集合</a>。</p>
<p>在将 Redis 与 Laravel 一起使用前，我们鼓励你通过 PECL 安装并使用 <a href="https://github.com/phpredis/phpredis" target="_blank" rel="noopener noreferrer">PhpRedis</a> ，尽管扩展安装起来更复杂，但对于大量使用 Redis 的应用程序可能会带来更好的性能。如果你使用 <a href="https://learnku.com/docs/laravel/10.x/sail" target="_blank" rel="noopener noreferrer">Laravel Sail</a>, 这个扩展已经事先在你的 Docker 容器中安装完成。</p>
<p>如果你不能安装 PHPRedis 扩展，你或许可以使用 composer 安装 predis/predis 包。Predis 是一个完全用 PHP 编写的 Redis 客户端，不需要任何额外的扩展：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require predis/predis</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>在你的应用中配置 Redis 信息，你要在 <code v-pre>config/database.php</code> 文件中进行配置。在该文件中，你将看到一个 <code v-pre>Redis</code> 数组包含了你的 Redis 配置信息。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'client'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLIENT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phpredis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'127.0.0.1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PASSWORD'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_DB'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'cache'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'127.0.0.1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PASSWORD'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CACHE_DB'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在你的配置文件里定义的每个 Redis 服务器，除了用 URL 来表示的 Redis 连接，都必需要指定名称 、 host （主机）和 port （端口）字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'client'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLIENT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phpredis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'tcp://127.0.0.1:6379?database=0'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'cache'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'tls://user:password@127.0.0.1:6380?database=1'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="配置连接方案" tabindex="-1"><a class="header-anchor" href="#配置连接方案"><span>配置连接方案</span></a></h4>
<p>默认情况下，Redis 客户端使用 <code v-pre>tcp</code> 方案连接 Redis 服务器。另外，你也可以在你的 Redis 服务配置数组中指定一个 <code v-pre>scheme</code> 配置项，来使用 TLS/SSL 加密：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'client'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLIENT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phpredis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'scheme'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'tls'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'127.0.0.1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PASSWORD'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_DB'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="集群" tabindex="-1"><a class="header-anchor" href="#集群"><span>集群</span></a></h3>
<p>如果你的应用使用 Redis 集群，你应该在 Redis 配置文件中用 <code v-pre>clusters</code> 键来定义集群。这个配置键默认没有，所以你需要在 <code v-pre>config/database.php</code> 配置文件中手动创建：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'client'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLIENT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phpredis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'clusters'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PASSWORD'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，集群可以在节点上实现客户端分片，允许你实现节点池以及创建大量可用内存。这里要注意，客户端共享不会处理失败的情况；因此，这个功能主要适用于从另一个主数据库获取的缓存数据。<br>
如果要使用 Redis 原生集群，需要把 <code v-pre>config/database.php</code> 配置文件下的 <code v-pre>options.cluster</code> 配置项的值设置为 <code v-pre>redis</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'client'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLIENT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phpredis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'cluster'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLUSTER'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'redis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'clusters'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="predis" tabindex="-1"><a class="header-anchor" href="#predis"><span>Predis</span></a></h3>
<p>要使用 Predis 扩展去连接 Redis， 请确保环境变量 <code v-pre>REDIS_CLIENT</code> 的值为 <code v-pre>predis</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'client'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLIENT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'predis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除默认的 <code v-pre>host</code> ，<code v-pre>port</code> ，<code v-pre>database</code> 和 <code v-pre>password</code> 这些服务配置选项外， Predis 还支持为每个 Redis 服务器定义其它的 <a href="https://github.com/nrk/predis/wiki/Connection-Parameters" target="_blank" rel="noopener noreferrer">连接参数</a>。如果要使用这些额外的配置项，可以在 config/database.php 配置文件中将任意选项添加到 Redis 服务器配置内：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PASSWORD'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'read_write_timeout'</span> <span class="token operator">=></span> <span class="token number">60</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="redis-facade-别名" tabindex="-1"><a class="header-anchor" href="#redis-facade-别名"><span>Redis Facade 别名</span></a></h4>
<p>Laravel 的 <code v-pre>config/app.php</code> 配置文件包含了 <code v-pre>aliases</code> 数组，该数组可用于定义通过框架注册的所有类别名。方便起见，Laravel 提供了一份包含了所有 facade 的别名入口；不过，<code v-pre>Redis</code> 别名不能在这里使用，因为这与 phpredis 扩展提供的 <code v-pre>Redis</code> 类名冲突。如果正在使用 <code v-pre>Predis</code> 客户端并确实想要用这个别名，你可以在 <code v-pre>config/app.php</code> 配置文件中取消对此别名的注释。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'aliases'</span> <span class="token operator">=></span> <span class="token class-name static-context">Facade</span><span class="token operator">::</span><span class="token function">defaultAliases</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">merge</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Redis'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Redis</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="phpredis" tabindex="-1"><a class="header-anchor" href="#phpredis"><span>phpredis</span></a></h3>
<p>Laravel 默认使用 phpredis 扩展与 Redis 通信。Laravel 用于与 Redis 通信的客户端由 <code v-pre>redis.client</code> 配置项决定，这个配置通常为环境变量 <code v-pre>REDIS_CLIENT</code> 的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'client'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLIENT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phpredis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 重设 Redis 配置项...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除默认的 <code v-pre>scheme</code> , <code v-pre>host</code> , <code v-pre>port</code> , <code v-pre>database</code> 和 <code v-pre>password</code> 的服务器配置选项外，<code v-pre>phpredis</code> 还支持以下额外的连接参数：<code v-pre>name</code> , <code v-pre>persistent</code> , <code v-pre>persistent_id</code> , <code v-pre>prefix</code> , <code v-pre>read_timeout</code> , <code v-pre>retry_interval</code> , <code v-pre>timeout</code> 和 <code v-pre>context</code> 。 你可以在 <code v-pre>config/database.php</code> 配置文件中将任意选项添加到 Redis 服务器配置内：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PASSWORD'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'read_timeout'</span> <span class="token operator">=></span> <span class="token number">60</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'context'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// 'auth' => ['username', 'secret'],</span></span>
<span class="line">        <span class="token comment">// 'stream' => ['verify_peer' => false],</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="phpredis-序列化和压缩" tabindex="-1"><a class="header-anchor" href="#phpredis-序列化和压缩"><span>phpredis 序列化和压缩</span></a></h4>
<p>phpredis 扩展可以配置使用各种序列化和压缩算法。可以通过设置 Redis 配置中的 <code v-pre>options</code> 数组进行配置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'client'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'REDIS_CLIENT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phpredis'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'serializer'</span> <span class="token operator">=></span> <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token constant">SERIALIZER_MSGPACK</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'compression'</span> <span class="token operator">=></span> <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token constant">COMPRESSION_LZ4</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 重设 Redis 配置项...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当前支持的序列化算法包括： <code v-pre>Redis::SERIALIZER_NONE</code> （默认）, <code v-pre>Redis::SERIALIZER_PHP</code> , <code v-pre>Redis::SERIALIZER_JSON</code> , <code v-pre>Redis::SERIALIZER_IGBINARY</code> 和 <code v-pre>Redis::SERIALIZER_MSGPACK</code> 。</p>
<p>支持的压缩算法包括： <code v-pre>Redis::COMPRESSION_NONE</code> （默认）, <code v-pre>Redis::COMPRESSION_LZF</code> , <code v-pre>Redis::COMPRESSION_ZSTD</code> 和 <code v-pre>Redis::COMPRESSION_LZ4</code> 。</p>
<h2 id="与-redis-交互" tabindex="-1"><a class="header-anchor" href="#与-redis-交互"><span>与 Redis 交互</span></a></h2>
<p>你可以通过调用 <code v-pre>Redis</code> <a href="https://learnku.com/docs/laravel/10.x/facades" target="_blank" rel="noopener noreferrer">facade</a> 上的各种方法来与Redis进行交互。 <code v-pre>Redis</code> facade 支持动态方法，所以你可以在facade上调用各种 <a href="https://redis.io/commands" target="_blank" rel="noopener noreferrer">Redis 命令</a> ,这些命令将直接传递给 Redis 。 在本例中，我们将调用 <code v-pre>Redis</code> facade 的 <code v-pre>get</code> 方法，来调用 Redis <code v-pre>GET</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Redis</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>View<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 显示给定用户的配置文件</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">show</span><span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">View</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user:profile:'</span><span class="token operator">.</span><span class="token variable">$id</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上所述，你可以在 <code v-pre>Redis</code> facade 上调用任意 Redis 命令。 Laravel 使用魔术方法将命令传递给 Redis 服务器。如果一个 Redis 命令需要参数，则应将这些参数传递给 <code v-pre>Redis</code> facade 的相应方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Redis</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$values</span> <span class="token operator">=</span> <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">lrange</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'names'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你也可以使用 <code v-pre>Redis</code> facade 上的 <code v-pre>command</code> 方法将命令传递给服务器，它接受命令的名称作为其第一个参数，并将值的数组作为其第二个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$values</span> <span class="token operator">=</span> <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'lrange'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="使用多个-redis-连接" tabindex="-1"><a class="header-anchor" href="#使用多个-redis-连接"><span>使用多个 Redis 连接</span></a></h4>
<p>你应用里的 <code v-pre>config/database.php</code> 配置文件允许你去定义多个 Redis 连接或者服务器。你可以使用 <code v-pre>Redis</code> facade 上的 <code v-pre>connection</code> 方法获得指定的 Redis 连接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$redis</span> <span class="token operator">=</span> <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'connection-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>要获取获取一个默认的 Redis 连接，你可以调用 <code v-pre>connection</code> 方法时，不带任何参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$redis</span> <span class="token operator">=</span> <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="事务" tabindex="-1"><a class="header-anchor" href="#事务"><span>事务</span></a></h3>
<p><code v-pre>Redis</code> facade 上的 <code v-pre>transaction</code> 方法对 Redis 原生的 <code v-pre>MULTI</code> 和 <code v-pre>EXEC</code> 命令进行了封装。 <code v-pre>transaction</code> 方法接受一个闭包作为其唯一参数。这个闭包将接收一个 Redis 连接实例，并可能向这个实例发出想要的任何命令。闭包中发出的所有 Redis 命令都将在单个原子性事务中执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Redis</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name class-name-fully-qualified static-context">Facades<span class="token punctuation">\</span>Redis</span><span class="token operator">::</span><span class="token function">transaction</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Redis</span> <span class="token variable">$redis</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$redis</span><span class="token operator">-></span><span class="token function">incr</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_visits'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$redis</span><span class="token operator">-></span><span class="token function">incr</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'total_visits'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
定义一个 Redis 事务时，你不能从 Redis 连接中获取任何值。请记住，事务是作为单个原子性操作执行的，在整个闭包执行完其命令之前，不会执行该操作。</p>
</blockquote>
<h4 id="lua-脚本" tabindex="-1"><a class="header-anchor" href="#lua-脚本"><span>Lua 脚本</span></a></h4>
<p><code v-pre>eval</code> 方法提供了另外一种原子性执行多条 Redis 命令的方式。但是，<code v-pre>eval</code> 方法的好处是能够在操作期间与 Redis 键值交互并检查它们。 Redis 脚本是用 <a href="https://www.lua.org/" target="_blank" rel="noopener noreferrer">Lua 编程语言</a> 编写的。</p>
<p><code v-pre>eval</code> 方法一开始可能有点令人劝退，所以我们将用一个基本示例来明确它的使用方法。 <code v-pre>eval</code> 方法需要几个参数。第一，在方法中传递一个 Lua 脚本（作为一个字符串）。第二，在方法中传递脚本交互中用到的键的数量（作为一个整数）。第三，在方法中传递所有键名。最后，你可以传递一些脚本中用到的其他参数。</p>
<p>在本例中，我们要对第一个计数器进行递增，检查它的新值，如果该计数器的值大于 5，那么递增第二个计数器。最终，我们将返回第一个计数器的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">eval</span><span class="token punctuation">(</span><span class="token operator">&lt;&lt;</span><span class="token operator">&lt;</span><span class="token string single-quoted-string">'LUA'</span></span>
<span class="line">    local counter <span class="token operator">=</span> redis<span class="token operator">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"incr"</span><span class="token punctuation">,</span> <span class="token constant">KEYS</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">if</span> counter <span class="token operator">></span> <span class="token number">5</span> then</span>
<span class="line">        redis<span class="token operator">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"incr"</span><span class="token punctuation">,</span> <span class="token constant">KEYS</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    end</span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> counter</span>
<span class="line"><span class="token constant">LUA</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'first-counter'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second-counter'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
请参考 <a href="https://redis.io/commands/eval" target="_blank" rel="noopener noreferrer">Redis 文档</a> 更多关于 Redis 脚本的信息。</p>
</blockquote>
<h3 id="管道命令" tabindex="-1"><a class="header-anchor" href="#管道命令"><span>管道命令</span></a></h3>
<p>当你需要执行很多个 Redis 命令时，你可以使用 <code v-pre>pipeline</code> 方法一次性提交所有命令，而不需要每条命令都与 Redis 服务器建立一次网络连接。 <code v-pre>pipeline</code> 方法只接受一个参数：接收一个 Redis 实例的闭包。你可以将所有命令发给这个 Redis 实例，它们将同时发送到 Redis 服务器，以减少到服务器的网络访问。这些命令仍然会按照发出的顺序执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Redis</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name class-name-fully-qualified static-context">Facades<span class="token punctuation">\</span>Redis</span><span class="token operator">::</span><span class="token function">pipeline</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Redis</span> <span class="token variable">$pipe</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token variable">$i</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token variable">$i</span> <span class="token operator">&lt;</span> <span class="token number">1000</span><span class="token punctuation">;</span> <span class="token variable">$i</span><span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$pipe</span><span class="token operator">-></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"key:<span class="token interpolation"><span class="token variable">$i</span></span>"</span><span class="token punctuation">,</span> <span class="token variable">$i</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="发布-订阅" tabindex="-1"><a class="header-anchor" href="#发布-订阅"><span>发布 / 订阅</span></a></h2>
<p>Laravel 为 Redis 的 <code v-pre>publish</code> 和 <code v-pre>subscribe</code> 命令提供了方便的接口。你可以用这些 Redis 命令监听指定「频道」上的消息。你也可以从一个应用程序发消息给另一个应用程序，哪怕它是用其它编程语言开发的，让应用程序和进程之间能够轻松进行通信。</p>
<p>首先，用 <code v-pre>subscribe</code> 方法设置一个频道监听器。我们将这个方法调用放到一个 <a href="https://learnku.com/docs/laravel/10.x/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令</a> 中，因为调用 <code v-pre>subscribe</code> 方法会启动一个常驻进程：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Commands</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Command</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Redis</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">RedisSubscribe</span> <span class="token keyword">extends</span> <span class="token class-name">Command</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 控制台命令的名称和签名</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'redis:subscribe'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 控制台命令的描述</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$description</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Subscribe to a Redis channel'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 执行控制台命令</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'test-channel'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">echo</span> <span class="token variable">$message</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在我们可以使用 <code v-pre>publish</code> 方法将消息发布到频道：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Redis</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/publish'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">publish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test-channel'</span><span class="token punctuation">,</span> <span class="token function">json_encode</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Adam Wathan'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通配符订阅" tabindex="-1"><a class="header-anchor" href="#通配符订阅"><span>通配符订阅</span></a></h4>
<p>使用 <code v-pre>psubscribe</code> 方法，你可以订阅一个通配符频道，用来获取所有频道中的所有消息，频道名称将作为第二个参数传递给提供的回调闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">psubscribe</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$channel</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$message</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">psubscribe</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'users.*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$channel</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$message</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/redis/14887" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/re...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/redis/14887" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/re...</a></p>
</blockquote>
</div></template>


